// pages/student/student.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop:0,
    currentPage:0,
    loadNumberLimit:0,
    inputSearchKey:'',
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
     await this.getInitData()
  }, 
  inputSearchKey(event: any) {
    this.setData({
      inputSearchKey: event.detail.value
    });
    console.log(event.detail.value);
    
  },

  async search() {
    try {
      const db =  wx.cloud.database();
      const _ = wx.cloud.database().command;
      let key = this.data.inputSearchKey;
      console.log("查询的内容", key)
      let res = await db.collection('orders').where(_.or([{
        studentName: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        classify: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        remark: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        price: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        currentPrice: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        times: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        subject: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        form: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },

      {
        surplus: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        unitPrice: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      ])).orderBy('_updateTime','desc').get()

      if (res.data.length === 0) {
        wx.showToast({
          title: '没有查询到结果',
          icon: 'none',
          duration:3000
        })
      } else {
        wx.showToast({
          title: '查询成功',
          icon: 'success'
        });
        this.setData({
          list: res.data 
        });
        // let takeExpressOrders = res.data as any;
        // takeExpressOrders.map((item)=> {
        //   let  receiverAddressString  = `${item.takeCode} ${item.name} ${item.phone} ${item.buildingName} ${item.dormitoryNumber}`
        //   item.receiverAddressString = receiverAddressString;
        // })
        // this.setData({
        //   takeExpressOrders:takeExpressOrders,
        // });
      }
    } catch (error) {
      console.log(error);
      wx.showToast({
        title: '查询出错',
        icon: 'none'
      })
    }
  },
  async getInitData(){
    wx.showLoading({title:'加载中',mask:true})
    let docsNumber = await  (await wx.cloud.database().collection('orders').orderBy('_updateTime','desc').count()).total;
    let loadNumberLimit =   Math.ceil(docsNumber/20);
    let list = await (await wx.cloud.database().collection('orders').orderBy('_updateTime','desc').limit(20).get()).data;
    console.log(loadNumberLimit,list);
    this.setData({
      loadNumberLimit:loadNumberLimit,
      list:list
    })
    wx.hideLoading()
  },
  async getMoreData(){
    let currentPage = this.data.currentPage;
    currentPage+=1;
    let tips = "加载第" + (currentPage+1) + "页";
    console.log("load page " + (currentPage+1));
    wx.showLoading({title:tips});
    let list = await (await wx.cloud.database().collection('orders').orderBy('_updateTime','desc').limit(20).skip(currentPage*20).get()).data;
    wx.hideLoading();
    // 将新一页的数据添加到原数据后面
    var originArticles = this.data.list;
    var newList = originArticles.concat(list);
    console.log(newList);
    this.setData({
      list: newList,
      currentPage: currentPage
    })
  },

  toAdd(){
    wx.navigateTo({url:'/pages/orderChoiceStudent/orderChoiceStudent'})
  },
  onPullDownRefresh() {
    console.log('目前刷新');
    this.setData({
      list:[],
      loadNumber:0,
    });
    this.getInitData()
  },
  onReachBottom() {
    console.log('已经到底了');
    let currentPage = this.data.currentPage;
    let loadNumberLimit = this.data.loadNumberLimit;
    if(currentPage>=loadNumberLimit){
      wx.showToast({'title':'已经到底了',duration:3000})
    }else{
      this.getMoreData()
    }
  },

  onPageScroll(ev){
    var _this = this;
    if (ev.scrollTop <= 0) {
      ev.scrollTop = 0;
    } 
    if (ev.scrollTop > this.data.scrollTop || ev.scrollTop == wx.getSystemInfoSync().windowHeight) {
     this.setData({
       hidden:true
     })
    } else {
      this.setData({
        hidden:false
      })
    }
    setTimeout(function () {
      _this.setData({
        scrollTop: ev.scrollTop
      })
    }, 0)
  },

  toStudentEdit(){
    wx.navigateTo({url:'/pages/studentEdit/studentEdit'})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

 

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */


  /**
   * 页面上拉触底事件的处理函数
   */


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})